package com.dotflat.xj.repository;

import com.dotflat.xj.domain.TargetProjectValue;
import io.lettuce.core.dynamic.annotation.Param;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface TargetProjectValueRepository extends JpaRepository<TargetProjectValue, String>, JpaSpecificationExecutor<TargetProjectValue> {

    @Query(value="SELECT * FROM xj_target_project_value  where   targetId = ?1 AND parentId != 0  and status =0",nativeQuery = true)
    List<TargetProjectValue> getTargetIdList( String targetId);

    @Query(value="SELECT * FROM xj_target_project_value  where   targetId = ?1 AND parentId = 0  and status =0",nativeQuery = true)
    List<TargetProjectValue> getTargetId( String targetId);


    @Query(value="SELECT * FROM xj_target_project_value  where   targetId = ?1 AND parentId != 0  and status =0",nativeQuery = true)
    List<TargetProjectValue> getTargetIdProject( String targetId);

    List<TargetProjectValue> findAllByTargetIdAndParentIdAndStatus( String targetId,String parentId,String status);

    @Query(value="SELECT t.id, t.created, t.deleted, t.areaId, t.audited,t.createdBy, t.targetId, t2.sortName remark, t.sortName, " +
            "t.sortValue, t.parentId, t.sortValueLow  FROM xj_target_project_value t LEFT JOIN xj_target_project_value t2 on t.parentId = t2.id  " +
            "where t.targetId = :targetId  AND  if(:sortName  !='', t.sortName like CONCAT('%', :sortName, '%'), 1=1)   ",nativeQuery = true)
    List<TargetProjectValue> projectList(@Param("targetId")String targetId, @Param("sortName")String sortName);

    @Modifying
    @Transactional
    @Query(value="update  xj_target_project_value set status = '1' where parentId = ?1 ",nativeQuery = true)
    void updateStatus(String parentId);

    @Modifying
    @Transactional
    @Query(value="delete from xj_target_project_value where  parentId = ?1 ",nativeQuery = true)
    void deleteParentId(String parentId);

    @Modifying
    @Transactional
    @Query(value="delete from xj_target_project_value where  id = ?1 ",nativeQuery = true)
    void deleteId(String id);

    List<TargetProjectValue> findAllByTargetIdAndParentId( String targetId,String parentId);
}
